package cn.edu.scau.cmi.yangqing.hibernate.client;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import cn.edu.scau.cmi.yangqing.hibernate.domain.Student;
import cn.edu.scau.cmi.yangqing.hibernate.domain.Tutor;

public class HibernateClient {
	private static SessionFactory factory;
	public static void main(String[] args) {
		Configuration configuration = new Configuration().configure();
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
				.applySettings(configuration.getProperties()).buildServiceRegistry();
		factory = configuration.buildSessionFactory(serviceRegistry);
		HibernateClient client = new HibernateClient();
		client.addTutor("bbb");
	}
	
	//添加学生
	public int addStudent(String name) {
		Session session =  factory.openSession();
		Transaction tx = null;
		int idStudent = 0;
		
		tx = session.beginTransaction();
		Student student = new Student();
		student.setName(name);
		idStudent  = (Integer) session.save(student);
		
		session.close();
		return idStudent;
	}
	//添加老师
	public int addTutor(String name) {
		Session session =  factory.openSession();
		Transaction tx = null;
		int idTutor = 0;
		
		tx = session.beginTransaction();
		Tutor tutor = new Tutor();
		tutor.setName(name);
		idTutor = (Integer) session.save(tutor);
		session.close();
		
		return idTutor;
	}
}
